#include "stdafx.h"
#include "net/SocketAddressImpl.hpp"

NET_NAMESPACE_BEGIN

//
// CSocketAddressImpl
//
CSocketAddressImpl::CSocketAddressImpl()
{
}

CSocketAddressImpl::~CSocketAddressImpl()
{
}


//
// CIPv4SocketAddressImpl
//
CIPv4SocketAddressImpl::CIPv4SocketAddressImpl()
{
	std::memset(&m_addr, 0, sizeof(m_addr));
	m_addr.sin_family = AF_INET;
	jh_set_sin_len(&m_addr);
}

CIPv4SocketAddressImpl::CIPv4SocketAddressImpl(const struct sockaddr_in* addr)
{
	std::memcpy(&m_addr, addr, sizeof(m_addr));
}

CIPv4SocketAddressImpl::CIPv4SocketAddressImpl(const void* addr, UInt16 port)
{
	std::memset(&m_addr, 0, sizeof(m_addr));
	m_addr.sin_family = AF_INET;
	std::memcpy(&m_addr.sin_addr, addr, sizeof(m_addr.sin_addr));
	m_addr.sin_port = port;
}

CIPAddress CIPv4SocketAddressImpl::host() const
{
	return CIPAddress(&m_addr.sin_addr, sizeof(m_addr.sin_addr));
}

UInt16 CIPv4SocketAddressImpl::port() const
{
	return m_addr.sin_port;
}

JH_SOCKETLEN CIPv4SocketAddressImpl::length() const
{
	return sizeof(m_addr);
}

const struct sockaddr* CIPv4SocketAddressImpl::addr() const
{
	return reinterpret_cast<const struct sockaddr*>(&m_addr);
}

int CIPv4SocketAddressImpl::af() const
{
	return m_addr.sin_family;
}



#ifdef JH_HAVE_IPv6
//
// CIPv6SocketAddressImpl
//
CIPv6SocketAddressImpl::CIPv6SocketAddressImpl(const struct sockaddr_in6* addr)
{
	std::memcpy(&m_addr, addr, sizeof(m_addr));
}

CIPv6SocketAddressImpl::CIPv6SocketAddressImpl(const void* addr, UInt16 port)
{
	std::memset(&m_addr, 0, sizeof(m_addr));
	m_addr.sin6_family = AF_INET6;
	jh_set_sin6_len(&m_addr);
	std::memcpy(&m_addr.sin6_addr, addr, sizeof(m_addr.sin6_addr));
	m_addr.sin6_port = port;
}

CIPv6SocketAddressImpl::CIPv6SocketAddressImpl(const void* addr, UInt16 port, UInt32 scope)
{
	std::memset(&m_addr, 0, sizeof(m_addr));
	m_addr.sin6_family = AF_INET6;
	jh_set_sin6_len(&m_addr);
	std::memcpy(&m_addr.sin6_addr, addr, sizeof(m_addr.sin6_addr));
	m_addr.sin6_port = port;
	m_addr.sin6_scope_id = scope;
}

CIPAddress CIPv6SocketAddressImpl::host() const
{
	return CIPAddress(&m_addr.sin6_addr, sizeof(m_addr.sin6_addr), m_addr.sin6_scope_id);
}

UInt16 CIPv6SocketAddressImpl::port() const
{
	return m_addr.sin6_port;
}

JH_SOCKETLEN CIPv6SocketAddressImpl::length() const
{
	return sizeof(m_addr);
}

const struct sockaddr* CIPv6SocketAddressImpl::addr() const
{
	return reinterpret_cast<const struct sockaddr*>(&m_addr);
}

int CIPv6SocketAddressImpl::af() const
{
	return m_addr.sin6_family;
}

#endif

NET_NAMESPACE_END
